package com.cloudcc.mobile.im.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.manager.UrlManager;
import com.cloudcc.mobile.util.UrlTools;
import com.cloudcc.mobile.util.UserUtils;
import com.cloudcc.mobile.view.activity.Myinformation;
import com.cloudcc.mobile.view.activity.UserInfoActivity;
import com.mypage.bean.CnumberBean;
import com.mypage.utils.SaveTemporaryData;

import java.util.List;

/**
 * Created by Administrator on 2017/8/8 0008.
 */

public class GroupMemperAdatper extends BaseAdapter {



    private Context mContext;
    private List<CnumberBean.NumberData> guanzhudatea;
    private LayoutInflater inflater;
    protected int[] colors;
    private String isguanzhu_x;

    public GroupMemperAdatper(Context mContext, List<CnumberBean.NumberData> guanzhudatea, String isguanzhu_x) {
        this.mContext = mContext;
        this.guanzhudatea = guanzhudatea;
        this.isguanzhu_x = isguanzhu_x;
        inflater = LayoutInflater.from(mContext);
        String[] array = mContext.getResources().getStringArray(
                R.array.logo_colors);
        colors = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            colors[i] = Color.parseColor(array[i]);
        }


    }


    @Override
    public int getCount() {
        return guanzhudatea.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        } else {
            return 1;
        }

    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public Object getItem(int position) {
        return guanzhudatea.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        GroupMemperAdatper.ViewHolder holder = null;
        if (getItemViewType(position) == 0) {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.cower_item_gz, null);
                RelativeLayout layoutBtn = (RelativeLayout) convertView
                        .findViewById(R.id.layoutBtn);
                TextView tvnum_guanzhu = (TextView) convertView.findViewById(R.id.num_guanzhu);
                TextView _guanzhu = (TextView) convertView.findViewById(R.id.tv_guanzhu);
                ImageView icon_guanzhu = (ImageView) convertView.findViewById(R.id.icon_guanzhu);


                icon_guanzhu.setImageResource(R.drawable.cloudtab_must_003_1);
                tvnum_guanzhu.setText("" + guanzhudatea.size());

                if ("detailmember".equals(isguanzhu_x)){
                    icon_guanzhu.setImageResource(R.drawable.groupmember);

                    _guanzhu.setText(mContext.getString(R.string.chengyuan));
                }else if ("guanzhu".equals(isguanzhu_x)) {
                    _guanzhu.setText(mContext.getString(R.string.zzgz));
                } else if ("beiguanzhu".equals(isguanzhu_x)) {
                    _guanzhu.setText(mContext.getString(R.string.beiguanzhu));
                } else {
                    _guanzhu.setText(mContext.getString(R.string.zzgz));
                }
            }
        } else {
            if (convertView == null) {
                holder = new GroupMemperAdatper.ViewHolder();
                convertView = inflater.inflate(R.layout.cower_item_x, null);
                holder.textView = (TextView) convertView
                        .findViewById(R.id.cower_item_text);
                holder.image = (ImageView) convertView
                        .findViewById(R.id.cower_image);
                holder.ll = (LinearLayout) convertView.findViewById(R.id.childll);
                holder.icon_guanzhu = (ImageView) convertView.findViewById(R.id.icon_guanzhu);
                convertView.setTag(holder);
            } else {
                holder = (GroupMemperAdatper.ViewHolder) convertView.getTag();
            }


            holder.ll.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
//                    Intent intent = new Intent(mContext,
//                            NewUserInfoActivity.class);
                    Intent intent = new Intent(mContext,
                            Myinformation.class);
                    SaveTemporaryData.General = true;
                    SaveTemporaryData.detailDyamic = "";
                    SaveTemporaryData.GeneralOne = true;
                    intent.putExtra(
                            UserInfoActivity.KEY_USERID, guanzhudatea.get(position - 1).id);
                    intent.putExtra("name",guanzhudatea.get(position - 1).name);
                    SaveTemporaryData.mSmart = "";
                    mContext.startActivity(intent);

                }
            });

            holder.textView.setText(guanzhudatea.get(position - 1).name);

            String uri = UrlTools.getTopImage(guanzhudatea.get(position - 1)
                    .id);
            int colorindex = position % colors.length;
            String mUrl = UrlManager.getInterfaceUrl().replace("distributor.action","chatterGroup.action");
//             uri = mUrl+"?m=showGroupImage&id="+guanzhudatea.get(position - 1).id;
            UserUtils.setLogoRound_z(uri, holder.image, colors[colorindex],
                    getNickRowName(guanzhudatea.get(position - 1).name), mContext);


        }
        return convertView;
    }

    /**
     * 获取用户的 简称 名称
     *
     * @return
     */
    public String getNickRowName(String name) {
        if (TextUtils.isEmpty(name)) {
            return "";
        }
        if (name.length() <= 2) {
            return name;
        } else {
            return name.substring(name.length() - 2, name.length());
        }

    }

    class ViewHolder {
        TextView textView;
        ImageView image;
        LinearLayout ll;
        ImageView icon_guanzhu;
    }











}
